package com.bigflexlib.x.Math
{
	
	
	
	
	
	public class MathUtil
	{
		
		
		
		public static const GOLDEN_RATIO:Number = ( 1 + Math.sqrt( 5 ) ) / 2;
		
		
		
		/**
		 * 
		 * 
		 * 
		 * @param start
		 * @param end
		 * @param fraction
		 * @return 
		 * 
		 */		
		public static function getIntermediateNumber( start:Number, end:Number, fraction:Number = 0.5 ):Number
		{
			return start - ( ( start - end ) * fraction );
		}
		
		
		
		
		public static function round( num:Number, decimalPositions:Number=0 ):Number
		{
		
			// if decimal spaces == 0, then we should round to the whole
			// if defimal spaces == 1, then we should multiply by 10, round and then divide by 10
			// and so on...

			var factor:int = decimalPositions==0 ? 1 : Math.pow( 10, decimalPositions );
			return Math.round(  num * factor ) / factor;
		
		}
		
		
		
		
		
		
		public static function similar( num1:Number, num2:Number, maxError:Number ):Boolean
		{
			return ( Math.abs( num1 - num2 ) <= maxError );
		}
		
		
		
		
		/**
		 * 
		 * Compares two Floating Point numbers for equality.
		 * 
		 * @param a
		 * @param b
		 * @return 
		 * 
		 */		
		public static function eqFP( a:Number , b:Number ):Boolean
		{
			return Math.abs( a - b ) <= 1e-6 * ( Math.abs( a ) + Math.abs( b ) );
		}		
		
		
		
		
		
		
		
		public static function randInt( low:int, top:int ):int
		{
			if ( low == top )
				return top;
			
			low--;
			
			return Math.ceil( low + ( Math.random( ) * ( top - low ) ) );
		}
		
		
		
		/**
		 * 
		 * yes / no random
		 * 
		 * @return 
		 * 
		 */		
		public static function coinToss( ):Boolean
		{
			return ( randInt( 0,1 ) == 1 );
		}
		

	}
	
}